package com.vercon.otamirror;

import android.os.Bundle;
import android.os.Looper;
import android.os.MessageQueue;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.vercon.commonmirror.Constants;
import com.vercon.commonmirror.activity.PermissionsActivity;
import com.vercon.commonmirror.cache.MirrorDataStorage;
import com.vercon.commonmirror.utils.APPUtil;
import com.vercon.commonmirror.utils.LogUtils;
import com.vercon.commonmirror.utils.RxTimer;
import com.vercon.otamirror.dialog.FirmwareUpdateDialog;
import com.vercon.otamirror.dialog.UpdateFirmwareDialog;
import com.vercon.otamirror.fragment.HomeFragment;
import com.vercon.otamirror.mode.FirmwareMode;
import com.vercon.otamirror.service.HomeSettingViewModel;
import com.vercon.otamirror.utils.helper.FragmentOpenManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;


/**
 * Created by csh on 2021/1/22
 * 主程序入口
 */
public class OtaActivity extends PermissionsActivity {

    private final String TAG = OtaActivity.class.getSimpleName();
    //    private static final String BUNDLE_FRAGMENTS_KEY = "androidx.fragment.app:fragments";
    public static final String SCREEN_OFF = "android.intent.action.vercon.SCREEN_OFF";//关屏广播
    public static final String SCREEN_ON = "android.intent.action.vercon.SCREEN_ON";//开屏广播


    @BindView(R.id.frame_vercon_layout_content)
    FrameLayout frameLayoutContent;

    public static HomeSettingViewModel homeSettingViewModel;
    private RxTimer mRxTimer;


    public static FragmentActivity content;
    public static String otaData = "otaData";



    @Override
    protected int setContentView(@Nullable Bundle savedInstanceState) {
        //虚拟键不顶起界面
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        return R.layout.activity_ota;
    }

    @Override
    protected void initView() {

        content = this;

        mRxTimer = new RxTimer();
        homeSettingViewModel = new ViewModelProvider(this,new ViewModelProvider.NewInstanceFactory()).get(HomeSettingViewModel.class);
        homeSettingViewModel.setOtaSwitchData(false);

        homeSettingViewModel.getOtaSwitchLiveData().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean){
                    finish();
                }
            }
        });

//        APPUtil.setSystem(Constants.version_product_version, "1");
        LogUtils.i(TAG, "系统当前桌面状态：" + APPUtil.getSystem(Constants.version_product_version, "--"));
        //需要先设置布局id
        FragmentOpenManager.getInstance().setLayoutId(R.id.frame_vercon_layout_content);

        FragmentOpenManager.getInstance().switchFragment(new HomeFragment(), OtaActivity.this);


        FirmwareMode mode = (FirmwareMode) getIntent().getSerializableExtra(otaData);
        LogUtils.e(" FotaApplication ota推送的升级 --已经激活");
        boolean isota = MirrorDataStorage.instance().getBoolean(Constants.STORAGE_KEY_OTA, false);
        LogUtils.e(" FotaApplication ota推送的升级" + "--isota=" + isota + "--FirmwareUpdateDialog.isShow=" + FirmwareUpdateDialog.isShow + "--UpdateFirmwareDialog.isUpdateIng=" + UpdateFirmwareDialog.isUpdateIng);
        if (!FirmwareUpdateDialog.isShow && !UpdateFirmwareDialog.isUpdateIng) {
            LogUtils.e(" FotaApplication ota推送的升级界面  弹出");
            FirmwareUpdateDialog hintUpdateDialog = new FirmwareUpdateDialog();
            Bundle bundle = new Bundle();
            bundle.putSerializable(Constants.updateFirmware, mode);
            hintUpdateDialog.setArguments(bundle);
            FragmentOpenManager.getInstance().addFragment(hintUpdateDialog, this);
        }
    }




    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");


    }




    @Override
    protected void initListener() {




    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void hasPermission() {
        //有权限

    }



    @Override
    protected void onDestroy() {
        content = null;
        LogUtils.e(" onDestroy");


        FragmentOpenManager.getInstance().onDestroy();

        if (mRxTimer != null) {
            mRxTimer.cancel();
        }



        super.onDestroy();
    }


}